/*
Peripheral id's page 34 datasheet
*/

#include <include/AT91SAM7S64.h>
#include <include/defines.h>
#include <mod/include/TWI.h>
#include <mod/include/LCD.h>
#include <mod/include/PIT.h>
#include <mod/include/CMD.h>
#include <mod/include/UART.h>
#include <mod/include/EEPROM.h>


void Delay (unsigned long a){
  while (--a!=0);
}

void INIT(void){
  unsigned int loop;
  p_pCKGR->CKGR_PLLR=0x10493F0E;             //Zet de juiste klok instellingen zodat de microcontroller op 48 Mhz werkt
  p_pSysc->SYSC_RSTC_RMR |= 0xA5000001;      //enable user reset 
  p_pPMC->PMC_PCER |= 0x04;                  //Enable Peripheral clock voor PIOA
  p_pPIO->PIO_OER =0x00000018;
  p_pPIO->PIO_SODR=0x00000018;
  p_pPIO->PIO_PER |= 0xFFFFFFFF;             //Maak alles IO
  p_pPIO->PIO_ODR |= 0xFFFFFFFF;             //Maak alles I
  p_pPIO->PIO_OER=0x08000000;
  p_pPIO->PIO_CODR=0x08000000;
  __ARMLIB_enableIRQ();
  loop=65535;
  while(loop--);
}

int main ( void )
{
  unsigned long int result[22],loop,loop1,i=0,adress=0x00;
  INIT();
  loop=2400000;
  while(loop--);

  IO_EXPENDER_1_Init();
  IO_EXPENDER_1_Write(0x00);
//   SPI_Init();
//   DAC_Init();
//   DAC_A(1024);
//   DAC_B(2048);
//   DAC_C(3072);

//   ADC_Init();
  LCD_Init();
//   IO_Expander_SPI_Init();
//   IO_Expander_SPI_TRISB_Write(0x00);
  UART0_Init(9600);
//   PIT_Start();
  while(1){
    //LCD_Writeln(&str);
    //DAC_B(1024);

    i++;
    if(i%2){
      //IO_EXPENDER_1_Direction(0x0F);
      }else{
        //IO_EXPENDER_1_Direction(0xF0);
    }
    //result[0]=IO_Expander_SPI_PORTB_Read();
    //EEPROM_Write(0xAA,0x01);
    loop=24000;
    while(loop--);
/*      for(loop=0;loop<22;loop++){
         result[loop]=IO_EXPENDER_Read(loop);
//         loop1=2400000;
//         while(loop1--);
//           DAC_D(loop);
      }*/
//      result=EEPROM_Read(0x00);
//      result[0] = ADC_Read(7);
//      UART0_Write(result[0]>>8);
//      UART0_Write(result[0]);
//      CMD_Process();
    LCD_Write(KEYBOARD_Read(),31);
  }
}


//46	400695,6522	149,73	60,10434783
//divide 46  multiply 150
//divide 46  multiply 300 pll div 2 (moet boven aantal meghertz uit komen
